﻿using Swifter.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace EventBus.Common
{
    public class SwifterJsonSerializer : ISerializer
    {
        /// <summary>
        /// 将指定类型的实例序列化为string
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SerializeObject(object value)
        {
            var strValue = JsonFormatter.SerializeObject(value);
            return strValue;
        }
        /// <summary>
        /// 将指定类型的实例序列化为Byte[]
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] SerializeObjectToByte(object value)
        {
            var strValue = JsonFormatter.SerializeObject(value);
            return Encoding.Default.GetBytes(strValue);
        }

        /// <summary>
        /// 将 Json 字符串反序列化为指定类型的实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string value)
        {
            return JsonFormatter.DeserializeObject<T>(value);
        }

        /// <summary>
        /// 根据类型解析json
        /// </summary>
        /// <param name="value"></param>
        /// <param name="type"></param>
        public static object DeserializeObject(string value, Type type)
        {
            var obj = JsonFormatter.DeserializeObject(value, type);
            return obj;
        }

    }
}
